package com.imooc.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.imooc.bean.Message;
import com.imooc.service.ListService;
import com.imooc.service.MaintainService;

/**
* Created By lijiliang on 2018年7月25日 上午9:19:45
*/
@SuppressWarnings("serial")
public class AddServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		//接收参数
		String id = req.getParameter("id");
		String command = req.getParameter("command");
		String description = req.getParameter("description");
		String content = req.getParameter("content");
		MaintainService maintainService = new MaintainService();
		Message message=new Message();
		message.setId(id);
		message.setCommand(command);
		message.setDescription(description);
		message.setContent(content);
		if(id!=null&&!"".equals(id)) {
			maintainService.update(message);
		}else {
			maintainService.save(message);
		}
		
		
		//向页面传值
		/*req.setAttribute("command", command);
		req.setAttribute("description", description);
		//查询消息列表并传给页面
		ListService listService = new ListService();
		req.setAttribute("messageList", listService.queryMessageList(command, description));
*/		//向页面跳转
		resp.sendRedirect("list.action");
		//req.getRequestDispatcher("/list.action").forward(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

